﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin1_Hotel : System.Web.UI.Page
{
    eStoreDataContext db = new eStoreDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        rpt_Category.DataSource = db.Hotels;
        rpt_Category.DataBind();
    }
    protected void lbt_Add_New_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = true;
        panel_Edit.Visible = false;
    }
    protected void lbt_ChinhSua_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = false;
        panel_Edit.Visible = true;

        int hotelid = int.Parse(((LinkButton)sender).CommandArgument.ToString());
        // tim 
        var hotel = db.Hotels.Single(c => c.HotelId == hotelid);
        // load thong tin
        txt_CatId_Edit.Text = hotelid.ToString();
        txt_Name_Edit.Text = hotel.Name;
        txt_Image_Edit.Text = hotel.Images;
    }
    protected void btnSaveEdit_Click(object sender, EventArgs e)
    {
        try
        {
            // tim 
            var hotel = db.Hotels.Single(c => c.HotelId == int.Parse(txt_CatId_Edit.Text));
            hotel.Name = txt_Name_Edit.Text;
            hotel.Images = txt_Image_Edit.Text;
            //3 Luu
            db.SubmitChanges();
            panel_Edit.Visible = false;
            Response.Redirect("Hotel.aspx");
        }
        catch (Exception ex)
        {
            if (ex.Message.Contains("Unique_Name_Hotel"))
            {
                Response.Write("<script> alert('Chỉnh sửa không thành công! Tên đã tồn tại!');</script>");
                txt_Name_Edit.Focus();
            }
        }
    }
    protected void btnxoa_Click(object sender, EventArgs e)
    {
        int hotelid = int.Parse(((LinkButton)sender).CommandArgument.ToString());
        //1. tim
        var hotel = db.Hotels.Single(c => c.HotelId == hotelid);
        //2. Xoa
        db.Hotels.DeleteOnSubmit(hotel);
        //3. luu
        db.SubmitChanges();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            var hotel = new Hotel
            {
                Name = txt_Name.Text,
                Images = txt_ImageAdd.Text,
            };
            //2.them vao
            db.Hotels.InsertOnSubmit(hotel);
            //3. luu
            db.SubmitChanges();
            txt_Name.Text = txt_ImageAdd.Text = "";
            panel_Add.Visible = false;
            Response.Redirect("Hotel.aspx");
        }
        catch(Exception ex)
        {
            if (ex.Message.Contains("Unique_Name_Hotel"))
            {
                Response.Write("<script> alert('Thêm mới không thành công! Tên đã tồn tại!');</script>");
            }
        }
    }
    protected void lbt_ClosAdd_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = false;
    }
   
    protected void lbt_ClosEdit_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = false;
    }
}